// define the namespaces
jmaki.namespace("jmaki.widgets.yahoo.tabbedview");

/**
 * Yahoo UI Tabbed View Widget
 *
 * @author Greg Murray
 *      original author of the widget
 * @author Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
 *      Updates to topic and glue publish/subscribe model and added methods for adding/removing/getting tabs
 *
 * @see http://developer.yahoo.com/yui/tabview/
 */
jmaki.widgets.yahoo.tabbedview.Widget = function(wargs) {
    
    var self = this;
    var uuid = wargs.uuid;
    var topic = "/yahoo/tabbedview";
    var tabs = [];
    var selected = 0;
    
    //default handler for onClick tabs
    this.onClick = function(e) {
        var label = e.target.innerHTML;
        var v = {id: uuid, value:{label:label}};
        jmaki.publish(topic + "/onClick", v);
    }
    
    //read the widget configuration arguments
    if (typeof wargs.args != 'undefined') {
        //overide topic name if needed
        if (typeof wargs.args.topic != 'undefined') {
            topic = wargs.args.topic;
        }  
    }
    
    self._tabView = new YAHOO.widget.TabView(wargs.uuid);
    
    if (wargs.selected) {
        selected = wargs.selected;
    }
    for(var _ii=0; _ii < wargs.value.tabs.length; _ii++) {
        var _row = wargs.value.tabs[_ii];
        if (!_row.url) {
            var _tid = wargs.uuid + '_tab_' + _ii;
            var h =  document.getElementById(wargs.uuid).parentNode.clientHeight - 38; // TODO : Get the true label height
            var w =  document.getElementById(wargs.uuid).parentNode.clientWidth - 2;
            var content = ("<div style='height:" + h + "px;width:" + w + "' id='" + _tid +"'>" + _row.content + "</div>");           
            var _r = new YAHOO.widget.Tab({
                label: _row.label,
                content: content,
                active: (_ii == selected)
            });
            _r.addListener('click', self.onClick); 
            self._tabView.addTab(_r);
            
        } else {
            var _tid = wargs.uuid + '_tab_' + _ii;
            // calculate height here
            var h =  document.getElementById(wargs.uuid).parentNode.clientHeight - 35; // TODO : Get the true label height
            var w =  document.getElementById(wargs.uuid).parentNode.clientWidth - 2;
            if (h <= 50) h = 300;
            var _r = new YAHOO.widget.Tab({
                label: _row.label,
                active: (_ii == selected)
            });                
            _r.addListener('click', self.onClick); 
            self._tabView.addTab(_r);                    
            
            var of = _row.overflow;
            if (typeof of == 'undefined') of = false;
            var iframe = _row.iframe;
            if (typeof iframe == 'undefined') of = false;                  
            var cv = _r.get('contentEl');
            
            cv.id = _tid;         
            var iargs = {
                target: cv,
                useIframe : iframe,
                overflow: of,
                topic : _row.topic,
                url : _row.url,
                startHeight : h,
                startWidth : w,
                autosize : true
            };           
            var dcontainer = new jmaki.DContainer(iargs);          
        }
    }
    
    /**
     * returns the current 'active' tab (YAHOO.widget.Tab)
     * @return active tab object (YAHOO.widget.Tab)
     */
    this.getActiveTab = function() {
        var activeTab = self._tabView.get("activeTab");
        return activeTab;
    }
    
    /**
     * Add a tab (static)
     * @param label tab label
     * @param content tab content
     */
    this.addTab = function(label, content) {
        var t = new YAHOO.widget.Tab({
            label: label,
            content: content,
            active: true
        });
        self._tabView.addTab(t);
        t.addListener('click', self.onClick); 
    }
    
    /** 
     * Remove a tab
     * @param tab a YAHOO.widget.Tab that is to be removed
     */
    this.removeTab = function(tab) {
        if(typeof tab != 'undefined') {
            self._tabView.removeTab(tab);
        } else {
            YAHOO.log("undefined tab at removeTab","warn");
        }
    }
    
    /**
     * Returns the tab at a given index
     * @param index tab index
     * @return a YAHOO.widget.Tab tab at that index
     */
    this.getTab = function(index) {
        return self._tabView.getTab(index);
    }
    
    /**
     * Returns the length of the tabs array
     */
    this.getLength = function() {
        return self._tabView.get("tabs").length;
    }
}